﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class ScreenMain : MonoBehaviour
{   
    public GameObject Pixel;
    // 屏幕实体预制体数组
    GameObject[,] pixels = new GameObject[PublicData.kSreenSize, PublicData.kSreenSize];

    // 蛇的 mypoint 点
    private List<MyPoint> _snakePoint;
    public List<MyPoint> snakePoint {
        set {
            _snakePoint = value;
            // refScreenMain();
        }
        get {
            return _snakePoint;
        }
    }
    // 生成的 食物 mypoint 数组
    private List<MyPoint> _foodsPoint;
    public List<MyPoint> foodsPoint {
        set {
            _foodsPoint = value;
            // refScreenMain();
        }
        get {
            return _foodsPoint;
        }
    }
    // 正在显示的 mypoint 数组点
    private List<MyPoint> _viewData = new List<MyPoint>();
    // 初始化方法
    public void init() {
        for (int i = 0; i < pixels.GetLength(0); i++) {
            for (int j = 0; j < pixels.GetLength(1); j++) {
                Vector3 vector3 = PublicData.myXY2sXY(new MyPoint(i, j));//new Vector3(i, j, 0);
                GameObject pixel = Instantiate(Pixel, vector3, Quaternion.identity) as GameObject;
                pixel.transform.parent = transform;
                pixel.SetActive(false);
                pixels[i, j] = pixel;
            }
        }
    }

    // 刷新屏幕
    public void refScreenMain() {
        //清除屏幕
        foreach (var item in _viewData) {
            GameObject pixel = pixels[item.x, item.y];
            pixel.SetActive(false);
        }

        _viewData.Clear();
        _viewData = snakePoint.Union(foodsPoint).ToList();

        foreach (var item in _viewData) {
            GameObject pixel = pixels[item.x, item.y];
            pixel.SetActive(true);
        }
    }
}
